<template>
  <div class="saveUpdate">
    <el-dialog :title="id?'编辑常用语配置':'新增常用语配置'" :visible.sync="showDialog" @close="$emit('update:showUpdate', 0)"
               :showUpdate="showUpdate" width="500px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="150px" size="mini">
        <dialog-wrapper :MaxWidth="400">
          <el-form-item label="模板类型：" prop="temp_type" :rules="[{ required: true, message: '模板类型不能为空'}]">
            <el-radio v-model="DetailInfo.temp_type" label="2"
                      :disabled="![1].includes($store.state.user.userinfo.role_id)?false: true">公用模板
            </el-radio>
            <el-radio v-model="DetailInfo.temp_type" label="1"
                      :disabled="[1].includes($store.state.user.userinfo.role_id)?false: true">个人模板</el-radio>
          </el-form-item>
          <el-form-item label="关联问题类型：" prop="type_id" :rules="[{ required: true, message: '关联问题类型不能为空'}]">
            <el-select v-model="DetailInfo.type_id" clearable placeholder="请选择" :disabled="id?true:false">
              <el-option v-for="item in pbtl" :key="item.id" :label="item.desc"
                         :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="模版名称：" prop="title" :rules="[{ required: true, message: '模版名称不能为空'}]">
            <el-input v-model="DetailInfo.title" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="模版内容：" prop="context" :rules="[{ required: true, message: '模版内容不能为空'}]">
            <el-input v-model="DetailInfo.context" type="textarea" :rows="8"
                      placeholder="请输入内容"></el-input>
          </el-form-item>

        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', 0)" size="small">取 消</el-button>
        <el-button v-if="id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定
        </el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import {getCommonStatementById, updateCommonStatementById, addCommonStatement} from '@/api'

  export default {
    components: {DialogWrapper},
    data() {
      return {
        showDialog: this.showUpdate,
        loading: false,
        DetailInfo: {},
        pbtl:[],
      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      id: {
        type: Number
      },
      problemsTypeList: {
        type: Array
      }
    },
    created() {
      if (this.id) {
        this.getDetail()
        this.pbtl=this.problemsTypeList
      }else{
        this.filterPTL();
      }
    },
    methods: {
      filterPTL(){
          this.pbtl=this.problemsTypeList.filter(item=>{
            if(item.id!=1) {//充值申诉
              return item
            }
          })
      }
      ,
      saveDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            addCommonStatement(this.DetailInfo).then(res => {
              if (res.status === 0) {
                this.$message({type: 'success', message: '添加成功'})
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      updateDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            updateCommonStatementById(this.id, Object.assign(this.DetailInfo, {temp_type: parseInt(this.DetailInfo.temp_type)})).then(res => {
              if (res.status === 0) {
                this.$message({type: 'success', message: '编辑成功'})
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      getDetail() {
        getCommonStatementById(this.id).then(res => {
          if (res.status === 0) {
            this.DetailInfo = res.data
            this.DetailInfo.temp_type = this.DetailInfo.temp_type.toString()
          }
        })
      },
    }
    ,
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      }
    }
  }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
